package com.start.cms.model;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class CmsDiyModelColumn {
    private static final String COLUMN_TEMPLATE = "`${columnName}` ${columnType}${length}  ${isNotNull} COMMENT '${comment}'";
    private String columnName;

    private int length;

    private boolean isNotNull;

    private String columnType;

    private String comment;

    public String getSQL() {
        return COLUMN_TEMPLATE.replace("${columnName}", columnName)
                .replace("${columnType}", columnType)
                .replace("${${length}}", calculationTypeLength(columnType, length))
                .replace("${isNotNull}", isNotNull ? "" : "DEFAULT NULL")
                .replace("${comment}", StringUtils.isEmpty(comment) ? "" : comment);
    }

    private static CharSequence calculationTypeLength(String columnType, int length) {
        if (length < 1) return "";
        String lowerCase = columnType.toLowerCase();
        switch (lowerCase) {
            case "int":
            case "integer":
            case "float":
            case "json":
            case "text":
            case "date":
            case "time":
            case "datetime":
            case "year":
            case "timestamp":
                return "";
        }
        return String.valueOf(length);
    }

}
